package com.me.launcher.tools;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;

/**
 * Created by Administrator on 2017/5/3 0003.
 */

public class AnimUtil {

    public static void ScaleAndAlphaAnimSet(View view, float scaleMax, int duration) {
        view.setPivotX(0);
        view.setVisibility(View.VISIBLE);
         /*
                AnimationSet相当于一个动画的集合，true表示使用Animation的interpolator
                false则是使用自己的。
                Interpolator 被用来修饰动画效果，定义动画的变化率，可以使存在的动画效果
                accelerated(加速)，decelerated(减速),repeated(重复),bounced(弹跳)等。
             */

        final AnimationSet animationSet = new AnimationSet(true);

        ScaleAnimation scaleAnimation = new ScaleAnimation(0, scaleMax, 1, 1);
        AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
        scaleAnimation.setDuration(duration);
        alphaAnimation.setDuration(duration);
        scaleAnimation.setFillAfter(true);
        alphaAnimation.setFillAfter(true);
        //将AlphaAnimation这个已经设置好的动画添加到 AnimationSet中
        animationSet.addAnimation(scaleAnimation);
        animationSet.addAnimation(alphaAnimation);
        animationSet.setFillAfter(true);
        //启动动画
        view.startAnimation(animationSet);
    }


    public static void ValueAnimatorHeighAndWidth(final View view, final boolean isheigh, final boolean iswidth, int from, int to, int duration) {
        ValueAnimator valueAnimatorBar = ValueAnimator.ofInt(from, to);

        valueAnimatorBar.addUpdateListener(valueAnimator -> {
            //获取当前的height值
            int h = (Integer) valueAnimator.getAnimatedValue();
            if (isheigh) {
                view.getLayoutParams().height = h;

            }
            if (iswidth) {
                view.getLayoutParams().width = h;
            }
            view.requestLayout();
        });
        valueAnimatorBar.setDuration(duration);
        //开始动画
        valueAnimatorBar.start();
//
    }

    // 动画实际执行
    public static void startPropertyAnim(View view, String propertyName, float from, float to, int duration) {
        view.clearAnimation();
        ObjectAnimator anim = ObjectAnimator.ofFloat(view, propertyName, from, to);
        anim.setDuration(duration);
        anim.start();
    }

    public static void ObjectAnimatorScale(View view, String propertyName, float from, float to, int duration) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, propertyName, from, to);
        animator.setDuration(duration);
        animator.start();
    }


}
